{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "本文的目的是为了在本地联网环境下部署好YOLOv8的训练环境"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "安装好python,当前需要的python版本为10、11\\\n",
    "安装好CUDA11.8"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "下载YOLOv8的源码,main分支即可\\\n",
    "https://github.com/ultralytics/ultralytics"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "进入下载的ultralytics根目录,在此处打开控制台并执行以下指令,用于安装对应的依赖(可以去除requirements.txt中的torch torchvision torchaudio,因为是cpu的):\\\n",
    "pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "进入到下载的源码,使用源码安装YOLOv8,在每次更改源码后,需要重启下python解释器(DataShell中,Environment->Stop Jupyter Server):\\\n",
    "pip install -e F:\\Project\\DeepLearning\\YOLOv8\\ultralytics"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "使用以下指令,可以查询到ultralytics的源是F:\\Project\\DeepLearning\\YOLOv8\\ultralytics:\\\n",
    "pip list"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "但是默认的pytorch是cpu版本的,需要卸载掉并安装GPU版本:\\\n",
    "卸载:\\\n",
    " pip uninstall torch torchvision torchaudio\\\n",
    "安装(注: Markdown的-有异常,所以需要使用\\进行转义,用于最后的正常显示):\\\n",
    " pip3 install torch torchvision torchaudio \\--index-url https://download.pytorch.org/whl/cu118"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "查看是否安装了pytorch:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-03-17T08:53:45.822696Z",
     "start_time": "2024-03-17T08:53:43.894311Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.2.1+cu118\n"
     ]
    },
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "print(torch.__version__)\n",
    "torch.cuda.is_available()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "测试下是否可以正常使用YOLOv8,使用自带的例子检测,第一次使用会自动下载模型,需要保证下面的路径存在图片文件\\\n",
    "yolo predict model=\"../material/yolov8n.pt\" source=\"../material/bus.jpg\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "\n",
    "使用python进行检测,因为使用了源码安装的方式,如果需要语法提示,在DataShell中,需要File->Add Director or Project添加F:\\Project\\DeepLearning\\YOLOv8\\ultralytics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-17T08:55:34.401838Z",
     "start_time": "2024-03-17T08:55:33.994090Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "image 1/1 F:\\Project\\DeepLearning\\Game\\MHTest0\\Train\\..\\..\\material\\bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 20.0ms\n",
      "Speed: 4.0ms preprocess, 20.0ms inference, 2.0ms postprocess per image at shape (1, 3, 640, 480)\n"
     ]
    }
   ],
   "source": [
    "from ultralytics import YOLO\n",
    "import cv2\n",
    "\n",
    "# 关于配置参数,例如此处的task,参考文件在ultralytics/ultralytics/cfg/default.yaml\n",
    "yolo = YOLO(\"../../material/yolov8n.pt\", task=\"detect\")\n",
    "# 检测并获得结果集\n",
    "results = yolo.predict(source=\"../../material/bus.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-17T08:55:35.668263Z",
     "start_time": "2024-03-17T08:55:35.632764Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "ultralytics.engine.results.Boxes object with attributes:\n\ncls: tensor([ 5.,  0.,  0.,  0.,  0., 11.], device='cuda:0')\nconf: tensor([0.8734, 0.8657, 0.8528, 0.8252, 0.2611, 0.2551], device='cuda:0')\ndata: tensor([[2.2871e+01, 2.3128e+02, 8.0500e+02, 7.5684e+02, 8.7345e-01, 5.0000e+00],\n        [4.8550e+01, 3.9855e+02, 2.4535e+02, 9.0270e+02, 8.6569e-01, 0.0000e+00],\n        [6.6947e+02, 3.9219e+02, 8.0972e+02, 8.7704e+02, 8.5284e-01, 0.0000e+00],\n        [2.2152e+02, 4.0580e+02, 3.4497e+02, 8.5754e+02, 8.2522e-01, 0.0000e+00],\n        [0.0000e+00, 5.5053e+02, 6.3007e+01, 8.7344e+02, 2.6111e-01, 0.0000e+00],\n        [5.8167e-02, 2.5446e+02, 3.2557e+01, 3.2487e+02, 2.5507e-01, 1.1000e+01]], device='cuda:0')\nid: None\nis_track: False\norig_shape: (1080, 810)\nshape: torch.Size([6, 6])\nxywh: tensor([[413.9370, 494.0589, 782.1314, 525.5631],\n        [146.9480, 650.6274, 196.7951, 504.1505],\n        [739.5966, 634.6107, 140.2473, 484.8495],\n        [283.2440, 631.6676, 123.4533, 451.7380],\n        [ 31.5035, 711.9840,  63.0069, 322.9179],\n        [ 16.3078, 289.6667,  32.4992,  70.4148]], device='cuda:0')\nxywhn: tensor([[0.5110, 0.4575, 0.9656, 0.4866],\n        [0.1814, 0.6024, 0.2430, 0.4668],\n        [0.9131, 0.5876, 0.1731, 0.4489],\n        [0.3497, 0.5849, 0.1524, 0.4183],\n        [0.0389, 0.6592, 0.0778, 0.2990],\n        [0.0201, 0.2682, 0.0401, 0.0652]], device='cuda:0')\nxyxy: tensor([[2.2871e+01, 2.3128e+02, 8.0500e+02, 7.5684e+02],\n        [4.8550e+01, 3.9855e+02, 2.4535e+02, 9.0270e+02],\n        [6.6947e+02, 3.9219e+02, 8.0972e+02, 8.7704e+02],\n        [2.2152e+02, 4.0580e+02, 3.4497e+02, 8.5754e+02],\n        [0.0000e+00, 5.5053e+02, 6.3007e+01, 8.7344e+02],\n        [5.8167e-02, 2.5446e+02, 3.2557e+01, 3.2487e+02]], device='cuda:0')\nxyxyn: tensor([[2.8236e-02, 2.1415e-01, 9.9383e-01, 7.0078e-01],\n        [5.9939e-02, 3.6903e-01, 3.0290e-01, 8.3584e-01],\n        [8.2651e-01, 3.6314e-01, 9.9965e-01, 8.1207e-01],\n        [2.7348e-01, 3.7574e-01, 4.2589e-01, 7.9402e-01],\n        [0.0000e+00, 5.0975e-01, 7.7786e-02, 8.0874e-01],\n        [7.1812e-05, 2.3561e-01, 4.0194e-02, 3.0081e-01]], device='cuda:0')"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对结果的框选\n",
    "results[0].boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-17T08:55:40.122991Z",
     "start_time": "2024-03-17T08:55:37.363721Z"
    }
   },
   "outputs": [],
   "source": [
    "# 查看结果集合的第一个元素\n",
    "\n",
    "# 使用openCV显示结果\n",
    "img = results[0].plot()\n",
    "cv2.imshow(\"a\",img)\n",
    "cv2.waitKey()\n",
    "cv2.destroyWindow(\"a\")\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "# 显示结果(DataShell中,pyplot显示颜色异常,可以在VsCode中执行或者使用OpenCV的图片)\n",
    "# plt.imshow(results[0].plot()[:,:,::-1])\n",
    "\n",
    "# 使用pyplot显示openCV结果\n",
    "# plt.imshow(img)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
